﻿// -----------------------------------------------------------------------
// <copyright file="JobsDescription_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class JobsDescription_DAL :BaseDAL
    {
        // Lấy tất cả JobDescription
        public static List<JobDescription> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.JobDescriptions);
            return DB.JobDescriptions.Where(n => (n.Ship_ID == shipID) && (n.Status == "Active")).ToList();
        }
        public static List<JobDescription> GetAllJob(int shipID)
        {
          //  DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.JobDescriptions);
            return DB.JobDescriptions.Where(n => (n.Ship_ID == shipID)).ToList();
        }

        // Lấy JobDescription theo Id
        public static JobDescription GetDataById(int id)
        {
            return DB.JobDescriptions.Where(m => m.JobDescription_ID == id).FirstOrDefault();
        }
        public static JobDescription GetNewJobDescription()
        {
            return DB.JobDescriptions.ToArray().LastOrDefault();
        }
        public static JobDescription GetDataByCode(string code, int shipID)
        {
            return DB.JobDescriptions.Where(m => (m.CodeJD == code) &&(m.Ship_ID == shipID)).FirstOrDefault();
        }
        // Lấy Maker theo Id
        public static JobDescription GetDataByName(string name, int shipID)
        {
            return DB.JobDescriptions.Where(m => (m.NameJD == name) && (m.Ship_ID == shipID)).FirstOrDefault();
        }
        //Thêm một JobDescription
        public static bool Insert(JobDescription JobDescription)
        {
            try
            {
                DB.JobDescriptions.InsertOnSubmit(JobDescription);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một JobDescription
        public static bool Delete(JobDescription JobDescription)
        {
            try
            {
                DB.JobDescriptions.DeleteOnSubmit(JobDescription);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật JobDescription
        public static bool Update(JobDescription jobDescription)
        {
            try
            {
                //JobDescription objJobDescription = GetDataById(jobDescription.JobDescription_ID);
                //objJobDescription.CodeJD = jobDescription.CodeJD;
                //objJobDescription.NameJD = jobDescription.NameJD;
                //objJobDescription.DetailsJD = jobDescription.DetailsJD;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
      
        // Kiểm tra xem JobDescription Code có tồn tại chưa ?
        public static bool IsExits(JobDescription jobDescription)
        {
            JobDescription objJobDescription = null;
            objJobDescription = DB.JobDescriptions.Where(m => (m.CodeJD == jobDescription.CodeJD) &&
                (m.JobDescription_ID != jobDescription.JobDescription_ID) && (m.Ship_ID == jobDescription.Ship_ID)).FirstOrDefault();
            return objJobDescription != null ? true : false;
        }
        public static bool IsRelationship(JobDescription jobDescription)
        {
            Job job = DB.Jobs.Where(n => n.JobDescription_ID == jobDescription.JobDescription_ID).FirstOrDefault();
            if (null != job)
                return false;
            Revision rev = DB.Revisions.Where(n => n.JobDescription_ID == jobDescription.JobDescription_ID).FirstOrDefault();
            if (null != rev)
                return false;

            return true;
        }

    }
}
